const HOST = require('../../../config.js').host

Page({
    data:{
        product: {},
        detail: {},
        expressList: {
            '1': '申通',
            '2': '顺丰',
            '3': '百世汇通'
        }
    },

    makePhoneCall () {
        wx.makePhoneCall({
            phoneNumber: '0571-86822802'
        })
    },

    getDetail (id) {
        let self = this
        wx.request({
            url: HOST + 'v1/userIndent/getListById/' + id,
            method: 'GET',
            success (res) {
                if (res.data.sysCode === 1) {
                    const data = res.data.data
                    console.log(data)
                    let map = {}
                    data.product.forEach(v => {
                        map[v.id] = v.mainImg
                    })

                    let product = data.userIndent.userIndentProduct
                    
                    product.forEach(v => {
                        let arr = v.productName.split(' ')
                        v.productName = arr[0]
                        v.amount = v.payCount
                        v.productPrice = v.payUnitPrice
                        let category = ''
                        arr.slice(1).forEach((v, idx) => {
                            if (idx !== 0 && idx % 2 !== 0) {
                                category += v + ' '
                            }
                        })
                        v.category = category
                        v.mainImg = map[v.productId]
                    })
                    data.userIndent.deliveryDate = data.userIndent.deliveryDate ? self.formateDate(data.userIndent.deliveryDate,'Y-M-D h:m') : "";
                    self.setData({
                        detail: data.userIndent,
                        product
                    })
                }
            }
        })
    },
    formatNumber(n) {  
      n = n.toString()  
      return n[1] ? n : '0' + n
    },  
    formateDate(number, format){
      var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
      var returnArr = [];

      var date = new Date(number);
      returnArr.push(date.getFullYear());
      returnArr.push(this.formatNumber(date.getMonth() + 1));
      returnArr.push(this.formatNumber(date.getDate()));

      returnArr.push(this.formatNumber(date.getHours()));
      returnArr.push(this.formatNumber(date.getMinutes()));
      returnArr.push(this.formatNumber(date.getSeconds()));

      for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
      }
      return format;  
    },

    onLoad (options) {
        this.getDetail(options.id)
    }
})